/**
 * 
 */
package com.ccqlk.attendance.controller.manage.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccqlk.attendance.common.page.PageRequest;
import com.ccqlk.attendance.controller.manage.AbstractManageController;
import com.ccqlk.attendance.model.Staff;
import com.ccqlk.attendance.model.User;
import com.ccqlk.attendance.repository.service.StaffService;
import com.ccqlk.attendance.repository.service.UserService;
import com.github.pagehelper.PageInfo;

/**
 * Copyright 2015-2018 重庆鲁控科技有限公司<br>
 * 文件标题: UserManageController<br>
 * 文件说明: TODO<br>
 * @author ZhangYinxian 348893009@qq.com<br>
 * 创建时间 2016年5月28日 下午11:38:21<br>
 */
@Controller
@RequestMapping("manage/system/user")
public class UserManageController extends AbstractManageController<User, Integer> {

	private UserService userService;
	
	private @Resource StaffService staffService;
	
	@Autowired
	public UserManageController(UserService userService) {
		super(userService, "manage/system/user/", "user");
		this.userService = userService;
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.controller.manage.AbstractManageController#getData(com.ccqlk.attendance.common.page.PageRequest, javax.servlet.http.HttpServletRequest, org.springframework.ui.Model, boolean)
	 */
	@Override
	public PageInfo<User> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		return userService.selectAll(pageRequest);
	}

	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.controller.manage.AbstractManageController#listHandle(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void listHandle(HttpServletRequest request) {
		List<Staff> staffs = staffService.selectAll();
		request.setAttribute("staffList", staffs);
	}
	
	/* (non-Javadoc)
	 * @see com.ccqlk.attendance.controller.manage.AbstractManageController#addHandle(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.BindingResult)
	 */
	@Override
	public void addHandle(HttpServletRequest request, User entity, BindingResult result) {
		if (entity!=null) {
			String password = entity.getPassword();
			password = new Md5Hash(password).toString();
			entity.setPassword(password);
		}
	}
	
	@RequestMapping("addJson")
	@ResponseBody
	@Override
	public Model addJson(HttpServletRequest request, User entity, Model model, BindingResult result) {
		model = super.addJson(request, entity, model, result);
		String code = StringUtils.leftPad(entity.getId()+"", 4, "0");
		entity.setCode(code);
		
		userService.edit(entity);
		
		return model;
	}
}
